package org.example.springbootdeptemp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.springbootdeptemp.dto.PageBean;
import org.example.springbootdeptemp.dto.PageResult;
import org.example.springbootdeptemp.mapper.EmpMapper;
import org.example.springbootdeptemp.pojo.Emp;
import org.example.springbootdeptemp.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public int updateEmps(Emp emp) {
        return empMapper.update(emp);
    }

    @Override
    public int deleteEmps(Integer[] ids) {
        return empMapper.delete(ids);
    }

    @Override
    public PageResult<Emp> selectPageEmps(PageBean<Emp> pageBean) {
        System.out.println(pageBean.getBegin());
        System.out.println(pageBean.getEnd());
        int pageNum = pageBean.getPageNum();
        int pageSize = pageBean.getPageSize();
        int totalPage = (pageNum - 1) * pageSize;
        pageBean.setTotalPage(totalPage);

        List<Emp> emps = empMapper.selectPageEmps(pageBean);
        Long count = (long) empMapper.count(pageBean);

        return new PageResult<Emp>(count,emps);
    }

    @Override
    public Emp selectEmpById(Integer id) {
        return empMapper.selectEmpById(id);
    }

    @Override
    public int insertEmp(Emp emp) {
        return empMapper.insertEmp(emp);
    }

    @Override
    public PageResult<Emp> getEmpsByPage(PageBean<Emp> pageBean) {
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        Page<Emp> empByPageBean = (Page<Emp>) empMapper.getEmpByPage(pageBean);
        return new PageResult<Emp>(empByPageBean.getTotal(), empByPageBean.getResult());

    }


}
